# coding:utf-8

import scrapy
from scrapy.selector import Selector
from ..items import ThreechardomainItem

class ThreeCharSpider(scrapy.Spider):
    name = 'threeChar'
    allowed_domains = [
        "chengyu.911cha.com"
    ]
    start_urls = [
        "https://chengyu.911cha.com/zishu_3.html"
    ]

    def parse(self, response):
        papers = response.xpath(".//*[@class='l5 center']/*")
        for paper in papers:
            word = paper.xpath(".//a/text()").extract()[0]
            # print word

            item = ThreechardomainItem(name=word)

            yield item

        next_page = Selector(response).re(u'<a href="(\S*)">下一页</a>')
        if next_page:
            yield scrapy.Request(url="https://chengyu.911cha.com/" + next_page[0], callback=self.parse)

        pass